package com.api.gbhg.huangwm.BxActionV2;

import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Huangwm
 * @Date 2022-11-07
 * @Version 1.0
 * @Description TODO 明细报销发起时校验项目经济分类额度，需要考虑合并同类项（即同一项目可能在一笔报销单内同时报销多个同一经济分类）
 **/
public class CheckQuotaForCreateDetails implements Action {
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/BxActionV2/CheckQuotaForCreateDetails",false);
    private BxActionUtils utils = new BxActionUtils();

    @Override
    public String execute(RequestInfo requestInfo) {
        int requestid = requestInfo.getRequestManager().getRequestid();
        logTool.writeLog("报销单requestid:"+requestid+" 进入提交后冻结逻辑");
        //报销信息都在明细1 只查询明细1
        //获取表名
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        int billid = requestInfo.getRequestManager().getBillid();
        //获取明细1表名
        DetailTable[] detailTables = requestInfo.getDetailTableInfo().getDetailTable();
        String dtTable_0 = detailTables[0].getTableDBName();

        //获取明细1数据集
        List<Map<String,Object>> datas = utils.getDetailsInfo_checkCreate(dtTable_0,billid);
        //第一步 先校验额度是否足够冻结
        logTool.writeLog("先校验额度是否足够冻结");
        RecordSet rs = new RecordSet();
        for(int i=0;i<datas.size();i++){
            Map<String,Object> data = datas.get(i);
            String ysxm = data.get("ysxm").toString();
            String bmjjzcfl = data.get("bmjjzcfl").toString();
            String sqbxje = data.get("sqbxje").toString();
            String sql = "select lcjlid,xmmc,bmzcjjfl,syed,(syed-("+sqbxje+"))as result from uf_jingfei where lcjlid="+ysxm+" and bmzcjjfl="+bmjjzcfl;
            logTool.writeLog(sql);
            if(rs.execute(sql) && rs.next()){
                Double syed = rs.getDouble("syed");
                Double result = rs.getDouble("result");
                String xmmc = rs.getString("xmmc");
                if(result<0){
                    requestInfo.getRequestManager().setMessagecontent(
                            "报销项目["+xmmc+"]中的经济分类["+bmjjzcfl+"]剩余额度["+syed+"]不足以扣减申请报销:"+sqbxje
                    );
                    requestInfo.getRequestManager().setMessageid(requestid+"");
                    return FAILURE_AND_CONTINUE;
                }
            }else {
                requestInfo.getRequestManager().setMessagecontent(
                        "报销项目["+utils.getProjectInfo(ysxm,"xmmin")+"]中的经济分类["+bmjjzcfl+"]未查询到经费记录"
                );
                requestInfo.getRequestManager().setMessageid(requestid+"");
                return FAILURE_AND_CONTINUE;
            }
        }

        //第二步 再根据创建人部门判断项目使用额度的控制
        List<Map<String,Object>> sydwdatas = new ArrayList<>();
        RecordSet rs2 = new RecordSet();
        User user = new User(requestInfo.getRequestManager().getCreater());
        int subid = user.getUserSubCompany1();
        logTool.writeLog("再根据创建人部门判断项目使用额度的控制 单位id="+subid);
        rs.execute("select sum(sqbxje-isnull(cxje,0)) as sqbxje,ysxm from "+dtTable_0+" where mainid="+billid+" group by ysxm");
        while (rs.next()){
            Map<String,Object> data = new HashMap<>();
            Double sqbxje = rs.getDouble("sqbxje");
            String ysxm = rs.getString("ysxm");
            String sql = "select isnull(djje,0)as djje,syed,ysyed from uf_ysxmsydwfped where xmbzlc=" + ysxm + " and sydw=" + subid;
            logTool.writeLog(sql);
            if(rs2.execute(sql) && rs2.next()){
                Double djje = rs2.getDouble("djje");
                Double syed = rs2.getDouble("syed");
                Double ysyed = rs2.getDouble("ysyed");
                if(sqbxje>syed){
                    requestInfo.getRequestManager().setMessagecontent("报销项目["+utils.getProjectInfo(ysxm,"xmmin")+"]中使用单位剩余额度["+syed+"]不足以报销:"+sqbxje);
                    requestInfo.getRequestManager().setMessageid(requestid+"");
                    return FAILURE_AND_CONTINUE;
                }
            }else{
                requestInfo.getRequestManager().setMessagecontent("报销项目["+utils.getProjectInfo(ysxm,"xmmin")+"]未查询到使用单位的额度分配记录(或查询出错) create");
                requestInfo.getRequestManager().setMessageid(requestid+"");
                return FAILURE_AND_CONTINUE;
            }
            data.put("ysxm",ysxm);
            data.put("sqbxje",sqbxje);
            sydwdatas.add(data);
        }
        //更新数据用
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        try {
            //第三步 生成报销记录以及冻结项目经济分类额度和项目使用单位额度
            boolean result1 = utils.syncDetailsRecords(rst,dtTable_0,billid,requestid);
            boolean result2 = utils.reduceJingFei(rst,datas);
            boolean result3 = utils.reduceSydwed(rst,sydwdatas,subid);
            logTool.writeLog("生成报销记录"+result1+" 冻结项目经济分类额度"+result2+" 冻结使用单位额度"+result3);
            if(!result1){
                rst.rollback();
                requestInfo.getRequestManager().setMessagecontent("明细报销创建提交节点后记录报销数据失败 ");
                requestInfo.getRequestManager().setMessageid(requestid+"");
                return FAILURE_AND_CONTINUE;
            }
            if(!result2){
                rst.rollback();
                requestInfo.getRequestManager().setMessagecontent("明细报销创建提交节点后冻结经济分类失败 ");
                requestInfo.getRequestManager().setMessageid(requestid+"");
                return FAILURE_AND_CONTINUE;
            }
            if(!result3){
                rst.rollback();
                requestInfo.getRequestManager().setMessagecontent("明细报销创建提交节点后冻结使用单位额度失败 ");
                requestInfo.getRequestManager().setMessageid(requestid+"");
                return FAILURE_AND_CONTINUE;
            }
        }catch (Exception e){
            rst.rollback();
            e.printStackTrace();
            requestInfo.getRequestManager().setMessagecontent("明细报销创建提交节点后动作出现异常 "+e.getMessage());
            requestInfo.getRequestManager().setMessageid(requestid+"");
            return FAILURE_AND_CONTINUE;
        }
        logTool.writeLog("报销单requestid:"+requestid+" 创建冻结经济分类额度成功");
        rst.commit();
        return SUCCESS;
    }
}
